<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Util {
	function __construct()
	{
		$this->CI = & get_instance();      
		$config = $this->consularray("SELECT clave,valor FROM config");
		foreach($config as $k=>$v)
			define('C_'.$k,$v);
	}
	function dameval($mpara,$data=array()){
		$qq = $this->CI->db->query($mpara,$data);
		$rr = $qq->row_array();
		$aa = each($rr);
		return $aa[1];
	}

	function damerow($mSQL,$data=array()){
		$query = $this->CI->db->query($mSQL,$data);
		$row=array();
		if ($query->num_rows() > 0)
			$row = $query->row_array();
		return $row;
	}
	
	function damerows($mSQL,$data=array()){
		$query = $this->CI->db->query($mSQL,$data);
		$rows=array();		
		if($query->num_rows() > 0){
			foreach ($query->result() as $fila) {
				$rows[] = $fila;
			}			    
		}
		return $rows;
	}

	function traeconfig($mvalor,$prede='',$descrip=''){
		$mvalor = $mvalor;
		$prede  = $prede;
		$descrip= $descrip;

		$this->CI->db->select('valor');
		$qq = $this->CI->db->get_where('config',array('clave'=>$mvalor));
		$rr = $qq->row_array();
		//print_r($rr);
		$aa = each($rr);
		if(count($rr)==0){
		  $this->CI->db->insert('config',array('clave'=>$mvalor,'valor'=>$prede,'descrip'=>$descrip));
		  return $prede;
		}else
		  return $aa[1];
	}

  function consularray($mSQL){
    $bote   = array();
    $ncampo = array();
    $mc     = $this->CI->db->query($mSQL);
    foreach ($mc->list_fields() as $field)
      array_push($ncampo, $field);
    if ($mc->num_rows() > 0){
      foreach( $mc->result_array() as $row )
        $bote[$row[$ncampo[0]]]=$row[$ncampo[1]];
    }
    return $bote;
  }
  
  function session_log($tabla,$oper){
      $data = array('id_sesion' => $this->CI->session->userdata("session_id"), 'tabla' => $tabla, 'observa' => $oper);
      $this->CI->db->insert('bitacora', $data);
  }

  function login($username, $password){
      //$query = $this->CI->db->get_where('usuario', array('login' => $username, 'password' => sha1($password)));
      $query = $this->CI->db->get_where('usuario', array('login' => $username, 'password' => $password));
      if($query->num_rows()>0){
          $this->CI->session->sess_destroy();
          $this->CI->session->sess_create();
          $this->CI->db->insert('sesion', array(
                                    'id'         => $this->CI->session->userdata('session_id'),
                                    'id_usuario' => $query->row()->id,
                                    'ip'         => $this->CI->session->userdata('ip_address'),
                                    'navegador'  => $this->CI->session->userdata('user_agent')
                                ));
          $this->CI->session->set_userdata(array(
                                               'ingresado'      => true,
                                               'login'          => $username,
                                               'id_usuario'     => $query->row()->id,
                                               'rol'            => $query->row()->rol
                                           ));

          return true;
      }
      else return false;
  }

  function logout(){
      $this->CI->session->sess_destroy();
      $this->CI->db->where('id', $this->CI->session->userdata('session_id'))->update('sesion', array('salida' => date("Y-n-j H:i:s"), 'ultima_act' => $this->CI->session->userdata('last_activity')));
  }
}
?>
